import { createRouter, createWebHistory } from 'vue-router'
import { useTabsStore } from '@/stores/tabs'


const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      // 登录
      path: '/login',
      name: 'Login',
      component: () => import('@/views/login/index.vue'),
    },
    {
      //主页
      path: '/',
      name: 'Home',
      component: () => import('@/views/Home.vue'),
      children: [
        {
          // 网站首页
          path: '/',
          name: 'SmartHealthcare',
          component: () => import('@/views/smart_healthcate/index.vue')
        },
        {
          // 智慧门诊
          path: '/smartClinic',
          name: 'SmartClinic',
          component: () => import('@/views/smart_clinic/index.vue')
        },
        {
          // 智慧门诊 —— 门诊预约
          path: '/doctorIntroduce',
          name: 'DoctorIntroduce',
          component: () => import('@/views/smart_clinic/DoctorIntroduce.vue')
        },
        {
          // 健康管理
          path: '/healthManagement',
          name: 'HealthManagement',
          component: () => import('@/views/health_management/index.vue'),
        },
        {
          // 健康管理 —— 健康详情
          path: '/healthDetail',
          name: 'HealthDetail',
          component: () => import('@/views/health_management/HealthDetail.vue')
        },
        {
          // 药品管理
          path: '/drugManagement',
          name: 'DrugManagement',
          component: () => import('@/views/drug_management/index.vue')
        },
        {
          // 药品管理 —— 药品介绍
          path: '/drugIntroduce',
          name: 'DrugIntroduce',
          component: () => import('@/views/drug_management/DrugIntroduce.vue')
        },
        {
          // 关于我们
          path: '/aboutUs',
          name: 'AboutUs',
          component: () => import('@/views/about_us/index.vue')
        },
        {
          // 个人中心
          path: '/personalCenter',
          name: 'PersonalCenter',
          component: () => import('@/views/personal_center/index.vue'),
          children: [
            {
              // 个人中心 —— 个人信息
              path: '/personalCenter',
              name: 'PersonalInfo',
              component: () => import('@/views/personal_center/PersonalInfo.vue')
            },
            {
              // 个人中心 —— 电子病历
              path: '/medicalRecord',
              name: 'MedicalRecord',
              component: () => import('@/views/personal_center/MedicalRecord.vue')
            },
            {
              // 个人中心 —— 预约状态
              path: '/appointmentStatus',
              name: 'AppointmentStatus',
              component: () => import('@/views/personal_center/AppointmentStatus.vue')
            },
            {
              // 个人中心 —— 修改密码
              path: '/changePassword',
              name: 'ChangePassword',
              component: () => import('@/views/personal_center/ChangePassword.vue')
            }
          ]
        }
      ],
      beforeEnter: (to, from, next) => {
        if (sessionStorage.getItem('id') === null) next('/login');
        else next();
      }
    },
    {
      //管理员
      path: '/userManager',
      name: 'Manager',
      component: () => import('@/views/management/index.vue'),
      children: [
        {
          //管理员 —— 用户管理
          path: '/userManager',
          name: 'UserManager',
          meta: {
            title: '用户管理'
          },
          component: () => import('@/views/management/UserManagement.vue')
        },
        {
          //管理员 —— 药品管理
          path: '/drugManager',
          name: 'DrugManager',
          meta: {
            title: '药品管理'
          },
          component: () => import('@/views/management/DrugManagement.vue')
        },
        {
          //管理员 —— 医生管理
          path: '/doctorManager',
          name: 'DoctorManager',
          meta: {
            title: '医生管理'
          },
          component: () => import('@/views/management/DoctorManagement.vue')
        },
        {
          //管理员 —— 健康管理
          path: '/healthManager',
          name: 'HealthManager',
          meta: {
            title: '健康管理'
          },
          component: () => import('@/views/management/HealthManagement.vue')
        }
      ],
      beforeEnter: (to, from, next) => {
        if (sessionStorage.getItem('id') === null) next('/login');
        else next();
      }
    }
  ]
});

router.beforeEach((to, from, next) => {
  const tabsStore = useTabsStore()

  // 排除不需要标签页的路由
  if (!to.meta.noCache) {
    tabsStore.addTab(to)
  }

  next()
})

export default router
